﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Data.UserDb;
using Acc.Lib;
using System.Text;

namespace Acc_Web.admin
{
    internal class Tmp
    {
        public Int32 CateId { get; set; }
        public String CateName { get; set; }
        public Int32 SrvId { get; set; }
    }

    public partial class AddItem : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                var sr = new ShopRepository();
                var categorySource = from c in sr.GetAllCategory() select new Tmp {CateId = c.CateId, CateName = c.CateName, SrvId = c.SrvId};

                var cateBindDta = from c in categorySource
                                  select new {Text = c.CateName, Value = c.Serialize().UrlEncoding(Encoding.Default)};
                _categoryList.DataSource = cateBindDta;
                _categoryList.DataTextField = "Text";
                _categoryList.DataValueField = "Value";
                _categoryList.DataBind();
            }
        }

        protected void _saveBtn_Click(object sender, EventArgs e)
        {
            var category = _categoryList.SelectedValue.UrlDecode(Encoding.Default).Deserialize<Tmp>();
            var item = new Items
                           {
                               CategoryId = category.CateId,
                               CreateDate = DateTime.Now,
                               ExpireDays = 500,
                               ItemImg = _itemImg.Text.Trim(),
                               ItemName = _itemName.Text.Trim(),
                               OnSale = true,
                               Price = Int32.Parse(_price.Text.Trim()),
                               VipPrice = Int32.Parse(_price.Text.Trim()),
                               Storage = Int32.Parse(_storage.Text.Trim()),
                               SrvId = category.SrvId,
                               TotalAmount = Int32.Parse(_totalAmount.Text.Trim())
                           };
            var sr = new ShopRepository();
            sr.CreateItem(item);
            sr.Save();
            Response.Redirect("~/admin/manageItem.aspx");
        }
    }
}
